#!/bin/bash
#定义函数
basepath=$(cd `dirname $0`;pwd)

INFO(){
echo -e "[`hostname` `whoami` `date '+%Y-%m-%d %H:%M:%S'`] $* " 
}
#检查image的是否存在
check_image()
{
  local image_name=$1
  INFO "check image [${image_name}]"
  local ret=`docker images --format {{.Repository}}:{{.Tag}} | grep ${image_name} | wc -l`
  local result=0
  if [ ${ret} -eq 0 ]; then
    INFO "not found image"
    result=1
  else 
    INFO "found image, image exits"
    result=0
  fi
  return ${result}
}
#提取标签
get_new_images(){
  image=$1
  number=`docker images --format {{.Repository}}:{{.Tag}}|grep ${registry}/${image}|awk -F ':' '{print $2}'|sed 's/v//g'|sort -r|head -1|sed 's/\.//g'`
  if test -z "$number";then
     [ $? -eq 0 ] && tag="v1.0.0" 
  else
     old_tag=`echo ${number}|cut -c 1`.`echo ${number}|cut -c 2`.`echo ${number}|cut -c 3`
     new_tag=`echo $(($number+1))|cut -c 1`.`echo $(($number+1))|cut -c 2`.`echo $(($number+1))|cut -c 3`
     if [ $? -eq 0 ];then
       tag="v${old_tag}"
     fi
  fi
}

#执行容器操作命令(start update status restart)
commands(){
  local image_name_tag=$1; local container=$2; local commds=$3
  for ii in $container;do
    #查看镜像是否被使ner_sner_shh用
    container_use_num=`docker ps -a|grep ${registry}/${name}:${images_latest_tag}|grep -v grep|wc -l`
    for i in $ii;do
    case ${commds} in
      "run")
       #重新run容器
        run "${sys_midd}" "run";;
      "start")
        container_sh  "${commds}" ${image_name_tag};;
      "stop")
        container_sh  "${commds}" ${image_name_tag};;
      "del")
        container_sh  "${commds}" ${image_name_tag};;
      "restart")
        container_sh "stop" ${image_name_tag}; sh container.sh "start" ;;
      "update")
        #判断容器是否使用最新的镜像启动，若已启动则跳过，否则重新run容器
        update  "run";;
      "status")
        #检查pcop开头容器状态
        status "${i}";;
    esac
    done
  done
}
check(){
  lists=$1;
  for name in $lists
   do
       if [[ $2 = "automatic" ]];then
                get_new_images "${name}"; commds="update";  images_latest_tag="${tag}"
          else
                images_latest_tag=`docker images|grep "${registry}/${name}"|awk '{print $2}'|sort -r|head -1`
       fi
       case "$name" in
            "prometheus")
             commands "${registry}/${name}:${images_latest_tag}" "prometheus" "$commds";;
            "grafana")
             commands "${registry}/${name}:${images_latest_tag}" "grafana" "$commds";;
            "node-exporter")
             commands "${registry}/${name}:${images_latest_tag}" "node-exporter" "$commds";;
       esac
  done
}

update(){
  is_container_exits=`docker ps -a|grep ${registry}/${name}:${images_latest_tag}|grep -v grep|grep $i|wc -l`
  is_container_up=`docker ps|grep ${registry}/${name}:${images_latest_tag}|grep -v grep|grep $i|wc -l`
  local commds=$1
  if [ $is_container_exits -eq 1 ];then
    [ $is_container_up -eq 0 ] && ERROR "latest image [${registry}/${name}:${images_latest_tag}] has been used by container [${i}],but container [${i}] is exited" \
                               || WARN "latest image [${registry}/${name}:${images_latest_tag}] has been used by container [${i}]"
  else
    if [ ! -d ${basepath}/template/${i} ];then
      ERROR "${mas_sla} not match ${i}" &&  break
    else
      cd ${basepath}/template/${i} && sh container.sh ${commds} ${image_name_tag}
      rm_old_images
    fi
  fi
}
container_sh(){
   local commds=$1; local image_name_tag=$2
  if [ -d ${basepath}/template/$i ];then
    cd ${basepath}/template/$i; sh container.sh ${commds} ${image_name_tag}
  else
    ERROR " [ $i ],please check"
  fi
}
